<template>
  <div class="app-container">

    <h2>全国景气指数</h2>
    <el-form v-for="(item,index) in entity.provinceData" :key="index" inline>
      <el-form-item label="城市">
        <el-input v-model="item.provinceName"></el-input>
      </el-form-item>
      <el-form-item label="指数">
        <el-input-number v-model="item.percent" :min="0" :max="100"></el-input-number>
      </el-form-item>
    </el-form>
    <h2>开放平台</h2>
    <el-form label-width="100px">
      <el-form-item label="今日求购数" prop="title">
        <el-input v-model="entity.todayBuy"></el-input>
      </el-form-item>
      <el-form-item label="今日搜索数" prop="title">
        <el-input v-model="entity.todaySearch"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveVirtualData()">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import VirtualAPI from '@/api/homepage/virtual';

export default {
    components: {},
    data() {
        return {
            loading: 0,
            entity: {
                globalSituation: 8.8,
                provinceData: new Array(5),
                'todayBuy': 17900,
                'todaySearch': 59000
            },
            example: {
                'globalPrice': 4598.0,
                'globalSituation': '成交一般',
                'provinceData': [{ 'provinceName': '湖南', 'percent': 100.0 }],
                'todayBuy': 17900,
                'todaySearch': 59000
            },
            rules: {},
            statusMap: [
                '草稿',
                '已发布'
            ]
        };
    },
    computed: {
        ...mapGetters([
            'dictionary'
        ])
    },
    created() {
        this.getVirtualData();
    },
    methods: {
        getVirtualData() {
            VirtualAPI.getVirtualData()
                .then((res) => {
                    this.entity = res.obj;
                    // 固定省份为5个
                    let current = this.entity.provinceData.length;
                    for (let i = current; i < 5; i++) {
                        this.entity.provinceData.push({ 'provinceName': '湖南', 'percent': 100.0 });
                    }
                });
        },
        saveVirtualData() {
            VirtualAPI.postVirtualData(this.entity)
                .then(() => {
                    this.$message.success('保存成功');
                    this.getVirtualData();
                });
        }
    }
};
</script>

<style>

</style>
